/*
 * TCSS 422 Web Spider Project
 * Group Money: Al McKenzie, Michael Pitts, Taylor Zielske
 */
package model;


import java.util.HashMap;
import java.util.Scanner;

/**
 * This class parses the data line by line looking for given search terms.
 * 
 * @author Al McKenzie
 * @version Oct 20, 2011
 */
public class WordSearchParser {
	
	/**
	 * The words to search for.
	 */
	private HashMap<String, Integer> hm = new HashMap<String, Integer>();
	
	
	/**
	 * The location of which words are found based 
	 * on string that was passed in. 
	 */
	private int[] location;
		
	/**
	 * Creates a new WordSearchParser which will parse a web page line by line.
	 * @param the_words are the words to search for.
	 */
	public WordSearchParser(final String[] the_words) {
		location = new int[the_words.length];
		int i =0;
		for(String d : the_words)
		{
			hm.put(d.toString().toLowerCase(), i++);
		}
		
	}
	
	/**
	 * Parses a single line from the page, looking for the search terms.
	 * @param the_line
	 */
	public void parseLine(final String the_line) {
		Scanner scan = new Scanner(the_line);
		scan.useDelimiter("[^a-zA-Z0-9\\-\']+");
		while(scan.hasNext())
		{
			String current = scan.next().toLowerCase();
			current = current.replace("'", "");	
			
			if(hm.containsKey(current))
			{
				location[hm.get(current)]++;
				
			}
		}
	}
	
	/**
	 * Returns the number of times each word has occurred so far.
	 * @return an integer array, where each index corresponds to a word.
	 */
	public int[] getData() {
		
		return location;
	}

}
